package com.yjt.termworks.adapter;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.yjt.termworks.LoginActivity;
import com.yjt.termworks.MainActivity;
import com.yjt.termworks.R;
import com.yjt.termworks.entity.HomeCircle;
import com.yjt.termworks.sql.MyHelper;
import com.yjt.termworks.utils.ImageLoaderUtils;
import com.yjt.termworks.utils.SharePreferenceData;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class HomeCircleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<HomeCircle> HomeCilrcleList;
    private OnItemClickListener mOnItemClickListener;

    public HomeCircleAdapter(Context context,List<HomeCircle> homeCircleList){
        this.mContext = context;
        this.HomeCilrcleList = homeCircleList;

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemview = LayoutInflater.from(mContext).inflate(R.layout.item_home_content,parent,false);
        RecyclerView.ViewHolder holder = new MyViewHolder(itemview);
        return holder;
    }


    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
        this.mOnItemClickListener = mOnItemClickListener;
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        HomeCircle homeCircle = HomeCilrcleList.get(position);
        //判断是否设置了监听器
            if(mOnItemClickListener != null){
                //为ItemView设置监听器
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition(); // 1
                        mOnItemClickListener.onItemClick(holder.itemView,position); // 2
                    }
                });
            }
//        if(holder instanceof MyViewHolder){

            // 朋友圈图片
        for (int i = 0; i < homeCircle.getCircleImagesList().size(); i++) {
                ImageView imageView = new ImageView(mContext);

                // 加载网络图片
                ImageLoaderUtils.display(mContext,imageView,homeCircle.getCircleImagesList().get(i));
//                imageView.setImageURI(Uri.parse(homeCircle.getCircleImagesList().get(i)));

                imageView.setLayoutParams(new ViewGroup.LayoutParams(130,130));
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                // 设置外边框
                ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(130,130); // 先创建一个只传宽度和高度的实例
                layoutParams.setMargins(0, 0, 122, 0); // 再设置外边距

                ((MyViewHolder) holder).contain.addView(imageView);
            }
            // 用户头像
             ImageLoaderUtils.display(mContext,((MyViewHolder) holder).avatar,homeCircle.getAvatar());

//            ((MyViewHolder) holder).avatar.setImageURI(Uri.parse(homeCircle.getAvatar()));
            ((MyViewHolder) holder).username.setText(homeCircle.getUsername());
            ((MyViewHolder) holder).circleMsg.setText(homeCircle.getCircleMsg());
            ((MyViewHolder) holder).likeNum.setText(homeCircle.getLikeNum());
            ((MyViewHolder) holder).circleComment.setText(homeCircle.getCircleComment());

//        }
    }


    @Override
    public int getItemCount() {
        return HomeCilrcleList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView avatar,circleImages;
        TextView username,circleMsg,likeNum;
        TextView circleComment;
        LinearLayout contain;
        public MyViewHolder(View view){
            super(view);
            avatar = view.findViewById(R.id.avatar);
            username = view.findViewById(R.id.username);
            circleMsg = view.findViewById(R.id.circleMsg);
            likeNum = view.findViewById(R.id.likeNum);
            circleComment = view.findViewById(R.id.circleComment);
            contain = view.findViewById(R.id.circleImages_Linear);
        }

    }
}
